package com.iflytek.edu.db.core.strategy.dm.grammar;

import com.alibaba.druid.sql.ast.SQLObject;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
import com.alibaba.druid.sql.ast.statement.SQLTableSource;
import com.iflytek.edu.db.core.constant.GrammarConstant;
import com.iflytek.edu.db.core.context.AdapterContext;
import com.iflytek.edu.db.core.strategy.GrammarStrategy;
import com.iflytek.edu.db.core.utils.AstUtils;

/**
 * @author linkai
 * @date 2022/2/11
 */
public class DmBacktickGrammarStrategy implements GrammarStrategy {

    @Override
    public boolean support(SQLObject expr) {
        if (expr == null) {
            return false;
        }
        if (expr instanceof SQLPropertyExpr) {
            // 参数 a.table_name
            SQLPropertyExpr expr1 = (SQLPropertyExpr) expr;
            return AstUtils.checkQuote(expr1.getName())
                    || support(expr1.getOwner());

        } else if (expr instanceof SQLIdentifierExpr) {
            // 标识 increment_sync
            SQLIdentifierExpr expr1 = (SQLIdentifierExpr) expr;
            return AstUtils.checkQuote(expr1.getName());

        } else if (expr instanceof SQLTableSource) {
            SQLTableSource expr1 = (SQLTableSource) expr;
            return AstUtils.checkQuote(expr1.getAlias());
        }
        return false;
    }

    @Override
    public void convert(SQLObject expr) {
        if (expr instanceof SQLPropertyExpr) {
            // 参数 a.table_name
            SQLPropertyExpr expr1 = (SQLPropertyExpr) expr;
            if (AstUtils.checkQuote(expr1.getName())) {
                expr1.setName(AstUtils.convertAlias(expr1.getName()));
            }
            convert(expr1.getOwner());
        } else if (expr instanceof SQLIdentifierExpr) {
            // 标识 increment_sync
            SQLIdentifierExpr expr1 = (SQLIdentifierExpr) expr;
            if (expr1.getName().startsWith(GrammarConstant.BACKTICK)) {
                expr1.setName(AstUtils.convertAlias(expr1.getName()));
            }
        } else if (expr instanceof SQLTableSource) {
            // 表名
            SQLTableSource expr1 = (SQLTableSource) expr;
            if (AstUtils.checkQuote(expr1.getAlias())) {
                expr1.setAlias(AstUtils.convertAlias(expr1.getAlias()));
            }
        }
    }

    @Override
    public void search(SQLObject expr) {
        if (expr instanceof SQLPropertyExpr) {
            // 参数 a.table_name
            SQLPropertyExpr expr1 = (SQLPropertyExpr) expr;
            if (AstUtils.checkQuote(expr1.getName())) {
                AdapterContext.addProblem(strategyType(), "语法问题：" + expr1.getName());
            }
            convert(expr1.getOwner());

        } else if (expr instanceof SQLIdentifierExpr) {
            // 标识 increment_sync
            SQLIdentifierExpr expr1 = (SQLIdentifierExpr) expr;
            if (expr1.getName().startsWith(GrammarConstant.BACKTICK)) {
                AdapterContext.addProblem(strategyType(), "语法问题：" + expr1.getName());
            }
        } else if (expr instanceof SQLTableSource) {
            // 表名
            SQLTableSource expr1 = (SQLTableSource) expr;
            if (AstUtils.checkQuote(expr1.getAlias())) {
                AdapterContext.addProblem(strategyType(), "语法问题：" + expr1.getAlias());
            }
        }

    }
}
